﻿package com.leqicheng.tenon.login.weibo;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.leqicheng.tenon.model.EntryInfoModel;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.exception.WeiboException;

public class AuthListener implements WeiboAuthListener {
    private final Context mContext;

    public AuthListener(Context context) {
        this.mContext = context;
    }

    @Override
    public void onComplete(Bundle values) {
        Oauth2AccessToken accessToken = Oauth2AccessToken
                .parseAccessToken(values);
        if (accessToken != null && accessToken.isSessionValid()) {
            String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
                    .format(new java.util.Date(accessToken.getExpiresTime()));
            // String format =
            // getString(R.string.weibosdk_demo_token_to_string_format_1);
            // mTokenView.setText(String.format(format, accessToken.getToken(),
            // date));
            String weiboUid = values.getString("uid");
            String weiboAccessToken = accessToken.getToken();
            EntryInfoModel model = EntryInfoModel.getInstance();
            model.setThirdPartUid(weiboUid);
            model.setThirdPartToken(weiboAccessToken);
            Intent intent = new Intent();
            intent.setAction("weibo success");
            mContext.sendBroadcast(intent);

            AccessTokenKeeper.writeAccessToken(this.mContext, accessToken);
        }
    }

    @Override
    public void onWeiboException(WeiboException e) {
        Toast.makeText(this.mContext, e.toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCancel() {
    }

}
